package com.example.lenovo.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.example.lenovo.myapplication.pojo.Sensor;

import java.util.ArrayList;

public class ceshi2 extends Fragment {
    private MyAdapter myAdapter;
    //定义最外层的表格对象
    private GridView gridView;
    //定义一个模拟数据
    private String[] title1=new String[]{"检测1","检测2","检测3","检测4"};

    //声明一个实体集合
    private ArrayList<Sensor> list=new ArrayList<>();
    //获取主框架的gridView控件页面并设置数据
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle bundle){
        //获取可操作的界面并实例化对象
        View view=inflater.inflate(R.layout.ceshi2,gridView,false);
        //
        gridView=(GridView) view.findViewById(R.id.ceshi2_gridview);

        list.clear();

        list.add(new Sensor("1","元",12.3));
        list.add(new Sensor("2","元",22.3));
        list.add(new Sensor("3","元",32.3));
        list.add(new Sensor("4","元",42.3));
        
        
        //实例化适配器并且传入
        myAdapter = new MyAdapter();
        gridView.setAdapter( myAdapter );
        return view;
    }


    //绑定数据的适配器
    public class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //设置并获取子页面的视图对象
            View view=LayoutInflater.from(getActivity()).inflate(R.layout.ceshi2_item_view,null);

            //获取页面中的可以改变的标题控件
            TextView textView0=(TextView)view.findViewById(R.id.ceshi2_textview_item_0);
            TextView textView1=(TextView)view.findViewById(R.id.ceshi2_textview_item_1);
            TextView textView2=(TextView)view.findViewById(R.id.ceshi2_textview_item_2);
            TextView textView3=(TextView)view.findViewById(R.id.ceshi2_textview_item_3);
            TextView textView4=(TextView)view.findViewById(R.id.ceshi2_textview_item_4);

            //从集合当中获取数据 并进行绑定
            String sensorName=list.get( position ).getSensorName();//获取集合当中sensorName当中所设置的值
            double data=list.get( position ).getData();
            String dataUnit=list.get( position ).getDataUnit();
            //通过值判断显示不同颜色
            if (data> 30){
                textView1.setText("报警");
                textView4.setTextColor(getResources().getColor(R.color.red));
                textView0.setBackgroundResource(R.drawable.textview_circle_darkgrey);
            }else{
                textView1.setText("正常");
                textView4.setTextColor(getResources().getColor(R.color.grey));
                textView0.setBackgroundResource(R.drawable.textview_circle_white);
            }

            textView3.setText( list.get(position).getDataUnit());
            textView4.setText( title1[position] );
            return view;
        }
    }
}
